package com.znf4.controller.platform.merchant;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.znf4.cache.redis.RedisUtils;
import com.znf4.constants.CommonConst;
import com.znf4.controller.platform.merchant.entity.Store;


/**
 * 类功能说明：商户信息管理
 */
@Controller
@RequestMapping("/paltform/merchant")
public class MerchantController {
	
	

	private static Log log = LogFactory.getLog(MerchantController.class);
	
	


	/**
	 * 函数功能说明 ：保存入注商户推广信息
	 */
//	@RequiresPermissions("agentMerchant:manager:edit")
	@RequestMapping(value = "/addStore", method = { RequestMethod.POST })
	public String addStore(HttpServletRequest request , Model model , Store store , String user_id ) {
		
		log.info("入注店铺信息 》" + store.toString() );
		
		String value = JSONObject.toJSONString(store) ;
		
		RedisUtils.addHash(CommonConst.MERCHANT_STORE_HASH_KEY , user_id , value) ;
		
		model.addAttribute("status", "操作完成");
		
		
		return "" ;
	}

	/**
	 * 函数功能说明 ：修改入注商户推广信息
	 */
	@RequestMapping(value = "/updateStore", method = RequestMethod.GET)
	public String updateStore(HttpServletRequest request , Model model , Store store , String user_id ) {
		
        log.info("修改店铺信息 》" + store.toString() );
		
		String value = JSONObject.toJSONString(store) ;
		
		RedisUtils.addHash(CommonConst.MERCHANT_STORE_HASH_KEY , user_id , value) ;
		
		model.addAttribute("status", "操作完成");
		
		return "merchant/merchantEditUI";
	}

	/**
	 * 函数功能说明 ：获取用户推广店铺信息
	 */
	@RequestMapping(value = "/queryStore", method = RequestMethod.GET)
	public String queryStore(HttpServletRequest request , Model model) {
		
		List<Store> lStore = new ArrayList<>() ;
		List<String> result = RedisUtils.getAllHash(CommonConst.MERCHANT_STORE_HASH_KEY ) ;
		for(int i = 0 ; i < result.size() ; i ++ ){
			
			Store store = JSON.parseObject( result.get(i), Store.class)  ;
			lStore.add(store) ;
		}
		
		model.addAttribute("data", lStore);
		return "merchant/merchantView";
	}



}
